package com.base.cn.platform.os.controller.mobile.index;

import com.base.cn.platform.os.common.MobileBaseController;
import com.base.cn.platform.os.common.constants.ConfigName;
import com.base.cn.platform.os.common.md5.MD5;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.count.PageFrom;
import com.base.cn.platform.os.common.utils.count.PageKeyUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.common.utils.web.WebUtils;
import com.base.cn.platform.os.service.course.cou.CourseService;
import com.base.cn.platform.os.service.course.open.CourseOpenService;
import com.base.cn.platform.os.service.manage.config.SysConfigService;
import com.base.cn.platform.os.service.manage.count.PageViewCountService;
import com.base.cn.platform.os.service.manage.op.advert.OpAdvertService;
import com.base.cn.platform.os.service.manage.op.article.OpArticleService;
import com.base.cn.platform.os.service.manage.op.groom.OpGroomDataService;
import com.base.cn.platform.os.service.manage.planTools.PopupSloganDetailService;
import com.base.cn.platform.os.service.manage.planTools.PopupSloganService;
import com.base.cn.platform.os.service.manage.subject.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * H5首页Controller
 *
 * @author s.li
 * @create 2018-05-11-17:34
 */
@Controller
public class MobileIndexController extends MobileBaseController {

    @Autowired
    private OpGroomDataService opGroomDataService;
    @Autowired
    private CourseOpenService courseOpenService;
    @Autowired
    private OpAdvertService opAdvertService;
    @Autowired
    private OpArticleService opArticleService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private PageViewCountService pageViewCountService;
    @Autowired
    private PopupSloganService popupSloganService;
    @Autowired
    private PopupSloganDetailService popupSloganDetailService;


    @RequestMapping("/")
    public String home(HttpServletRequest request){
        return redirect(request,"/mobile/index");
    }

    @RequestMapping("/mobile/index")
    public String index(HttpServletRequest request){
        TreeMap<String,String> oNoffMap = sysConfigService.getSysConfigContext(ConfigName.ONOFF);
        request.setAttribute("oNoffMap",oNoffMap);
        pageViewCountService.addPageCount(PageKeyUtils.INDEX, PageFrom.PAGE_FROM_MOBILE);
        return resultPage("/index/index");
    }

    @RequestMapping("/mobile/mobileError")
    public String mobileError(){
        return "/common/error";
    }

    //临时跳转路径,可删除，开始-------------
    @RequestMapping("/mobile/new/index")
    public String newIndex(){
        return resultPage("/newPage/index");
    }
    @RequestMapping("/mobile/new/newLiveIndex")
    public String newLiveIndex(){
        return resultPage("/newPage/live-index");
    }
    @RequestMapping("/mobile/new/newLiveInfo")
    public String newLiveInfo(){
        return resultPage("/newPage/live-info");
    }
    @RequestMapping("/mobile/new/informationList")
    public String newInformationList(){
        return resultPage("/newPage/information-list");
    }
    //临时跳转路径,可删除，结束-------------

    /**
     * H5首页banner查询
     * @return String
     */
    @RequestMapping("/mobile/index/bannerList")
    public String bannerList(Model model){
        Map<String, Object> params = new HashMap<>();
        params.put("placeKey", "APPBANNER");
        params.put("status",1);
        List<Map<String, Object>> advertList = opAdvertService.queryAdvertList(params, false, false, false);
        model.addAttribute("advertList", advertList);
        return resultPage("/index/index-banner");
    }

    /**
     * 查询H5首页资讯推荐列表
     * @return Map<String,Object>
     */
    @RequestMapping("/mobile/index/noticeList")
    @ResponseBody
    public Map<String,Object> noticeList(){
        Map<String, Object> params = new HashMap<>();
        params.put("placeKey", "IPA");
        List<Map<String,Object>> articleList = opGroomDataService.queryGroomDataList(params);
        return ResultUtil.SUCCESS(articleList);
    }

    /**
     * 查询首页推荐专业列表
     * @return Map<String,Object>
     */
    @RequestMapping("/mobile/index/queryGroomSubjectList")
    @ResponseBody
    public Map<String,Object> queryGroomSubjectList(){
        List<Map<String,Object>> subjectList = subjectService.findH5IndexSubjectList();
        return ResultUtil.SUCCESS(subjectList);
    }

    /**
     * H5端首页查询推荐数据
     * @param model
     * @param placeKey 推荐位置标识
     * @return
     */
    @RequestMapping("/mobile/index/queryGroomDataList")
    public String queryGroomDataList(Model model,
                                     @RequestParam(value = "placeKey") String placeKey){
        List<Map<String, Object>> dataList = opGroomDataService.findAppH5IndexGroomData(placeKey);
        model.addAttribute("dataList", dataList);
        model.addAttribute("placeKey",placeKey);
        if(placeKey.equals("IBC") || placeKey.equals("IHC")){
            return resultPage("/index/groom-course-list");
        } else if(placeKey.equals("SMALL")){
            return resultPage("/index/groom-small-list");
        } else {
            return resultPage("/index/groom-live-list");
        }
    }

    /**
     * H5端查询免费直播列表
     * @param model
     * @return 数据显示列表
     */
    @RequestMapping("/mobile/index/queryOpenCourseList")
    public String queryOpenCourseList(Model model,
                                      HttpServletRequest request){
        Map<String, Object> condition = new HashMap<>();
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        condition.put("userId",userId);
        condition.put("count",2);
        condition.put("openLive",1);
        condition.put("status",1);
        List<Map<String, Object>> courseOpenList = courseOpenService.findCourseOpenList(condition, false ,false, true, true);
        model.addAttribute("dataList",courseOpenList);
        return resultPage("/index/groom-open-course-list");
    }

    /**
     * H5端查询推荐专业下的课程列表
     * @param model
     * @return
     */
    @RequestMapping("/mobile/index/queryRecommendSubjectCourseList")
    public String queryRecommendSubjectCourseList(Model model){
        List<Map<String,Object>> subjectCourseList = courseService.findRecommendCourseList("AHBSC",true,true);
        model.addAttribute("subjectCourseList",subjectCourseList);
        return resultPage("/index/groom-subject-course-list");
    }

    /**
     * web首页banner查询
     * @return String
     */
    @RequestMapping("/mobile/index/savePageView")
    @ResponseBody
    public void savePageView(){
        pageViewCountService.addPageCount(PageKeyUtils.ACTIVITY_INFO, PageFrom.PAGE_FROM_MOBILE);
    }

    /**
     *
     * @return String
     */
    @RequestMapping("/mobile/index/appTestPage")
    public String appTestPage(){
        return resultPage("/index/index-appTestPage");
    }

    /**
     * 获取是否有弹框
     */
    @RequestMapping("/mobile/index/getIndexPopupSlogan")
    @ResponseBody
    public Map getPopupSlogan(HttpServletRequest request, HttpServletResponse response) {
        String userCookie_original = WebUtils.getCookie(request,"_popupSlogan");
        String userId = StringUtils.isEmpty(userCookie_original) ? MD5.getMD5(String.valueOf(System.currentTimeMillis())) : userCookie_original;
        Map<String, Object> resultMap = popupSloganService.getIndexPopupSlogan("mobile",userId);
        if ((boolean)resultMap.get("success") && StringUtils.isEmpty(userCookie_original)) {
            WebUtils.setCookie(response,"_popupSlogan", userId, 100);
        }
        return resultMap;
    }

    /**
     * 添加弹框点击
     * @param id
     * @return
     */
    @RequestMapping("/mobile/index/addClick")
    @ResponseBody
    public Map addClick(@RequestParam("id") BigDecimal id) {
        popupSloganDetailService.updateClick(id);
        return ResultUtil.SUCCESS("");
    }
}
